package comm;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Properties;

import javax.swing.JTextPane;

import org.apache.http.HttpResponse;

import View.VisualDbWindow;

public abstract class AppInterface 
{
	    /* Data Field Names for request insertion */
	    public static String idDevice           = "InfoDevice";
	    public static String idMeasure          = "InfoMeasure";
	    public static String value              = "InfoValue";
	    public static String actionName         = "action";
	    
	    /* Definitions read propeties file*/
	    public static Properties prop; 
	    public static InputStream streamProp;
	    
	    /*Char definition used*/
	    public static String portSeparator      =":";
	    public static String slashSeparator     ="/";
	    public static String sepParameters      ="&";
	    public static String beginParameters    ="?";
	    public static String assignValue        ="=";
	    
	    public static boolean hasDebug          = false;
	    
	    /*path files*/
	    public static String filePropeties      = "\\\\propeties\\configuracion.properties";
	    
	    /*path root */
	    public static String getRootPath()
	    {
	      return System.getProperty("user.dir");
	    }
	    
	    public static void setHasDebug(boolean debug)
	    {
	       hasDebug=debug;
	    }
	    
	    /*Access Config Properties*/
	    public static String getPropertyByName(String name)
	    {
	       if (prop==null)
	       {
	    	   try {
				      streamProp = new FileInputStream(getRootPath()+filePropeties);
				      prop = new Properties();
			       } 
	    	   catch (FileNotFoundException e) 
			       {
				      e.printStackTrace();
			       }
	           
	    	   try {
			          prop.load(streamProp);
		           } 
	    	   catch (IOException e) 
	    	       {
			         e.printStackTrace();
		           }   
	       }
	       
	      return prop.getProperty(name);
	   }
	    
	   public static void getAllProperties(JTextPane text)
	   {
		   String solution;
		   
		   if (prop==null)
	       {
	    	   try {
				      streamProp = new FileInputStream(getRootPath()+filePropeties);
				      prop = new Properties();
			       } 
	    	   catch (FileNotFoundException e) 
			       {
				      e.printStackTrace();
			       }
	           
	    	   try {
			          prop.load(streamProp);
		           } 
	    	   catch (IOException e) 
	    	       {
			         e.printStackTrace();
		           }   
	       }
		   
		   for (Enumeration<Object> e = prop.keys(); e.hasMoreElements() ; ) 
		   {
	            // Obtenemos el objeto
	            Object obj = e.nextElement();
	            text.setText(text.getText()+"\n"+obj + ": " + prop.getProperty(obj.toString()));
	        }
	   }
	    
	   /*Make Url for request*/
	   public static String FormedCallRequest()
	   {
		 String serverProtocol = getPropertyByName("serverdbProtocol");
		 String serverName     = getPropertyByName("serverdbCrown");
		 String port           = getPropertyByName("portdbCrown");
		 String path           = getPropertyByName("pathdbCrown");
		 String action         = getPropertyByName("actiondbCrown");

		 String solutionCall       = serverProtocol+serverName+portSeparator+port+slashSeparator+path;
		 
		 return solutionCall;
	   }
	   
	   public static void PrintRequest(String requestParam)
	   {
		 if (hasDebug) VisualDbWindow.setTxtAreaRealiza(requestParam);
		 else System.out.println(requestParam);
	   }
	   
	   public static void PrintResponse(HttpResponse response)
	   {
		   BufferedReader rd;
		   String line = "";
		try {
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			  while ((line = rd.readLine()) != null) 
			  {
				  if (hasDebug) VisualDbWindow.setTxtAreaRecibe(line);
				  else System.out.println(line);
			  }
		    } 
		catch (Exception e) 
		    {		  
			  e.printStackTrace();
		    } 
		
	   }     
	   

}
